package com.exam.classes.controller;

import com.exam.classes.pojo.Classes;
import com.exam.classes.service.ClassesService;
import com.exam.common.entity.Request;
import com.exam.common.entity.StatusCode;
import com.exam.common.exception.ExamException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName : ClassesController
 * @Description : 班级
 * @Author : WHB
 */

@RestController
@Api(description = "班级API接口")
public class ClassesController {

    @Autowired
    private ClassesService classesService;

    @PostMapping("/classes")
    @ApiOperation(value = "创建班级",notes = "创建班级")
    public Request createClasses(@RequestBody Map<String,String> data){
        Long tid = Long.valueOf(data.get("tid"));
        String cname = data.get("cname");
        try {
            classesService.createClasses(tid,cname);
            return new Request(true, StatusCode.OK,"创建班级成功");
        }catch (ExamException e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"创建班级失败");
        }
    }

    @DeleteMapping("/classes")
    @ApiOperation(value = "删除班级",notes = "删除班级")
    public Request deleteClasses(@RequestParam("cid") int cid){
        try {
            classesService.deleteClasses(cid);
            return new Request(true, StatusCode.OK,"删除班级成功");
        }catch (ExamException e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"删除班级失败");
        }
    }

    @GetMapping("/classes/{id}")
    @ApiOperation(value = "通过ID查询班级",notes = "通过ID查询班级")
    public Request getClasses(@PathVariable("id") Integer id){
        try {
            classesService.getClasses(id);
            return new Request(true, StatusCode.OK,"通过ID查询班级成功");
        }catch (ExamException e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"通过ID查询班级失败");
        }
    }

    @GetMapping("/classes/key/{joinKey}")
    @ApiOperation(value = "通过Key查询班级id",notes = "通过Key查询班级id")
    public Request queryClassesByKey(@PathVariable("joinKey") String joinKey){
        try {

            return new Request(true, StatusCode.OK,"通过Key查询班级成功", classesService.queryClassesByKey(joinKey));
        }catch (ExamException e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"通过Key查询班级失败");
        }
    }

    @GetMapping("/classes/list/{sid}")
    @ApiOperation(value = "通过sid查询班级列表",notes = "通过sid查询班级列表")
    public Request queryClassesListBySid(@PathVariable("sid") Long sid){
        try {
            return new Request(true, StatusCode.OK,"通过sid查询班级列表成功", classesService.queryClassesListBySid(sid));
        }catch (ExamException e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"通过sid查询班级列表失败");
        }
    }

    @GetMapping("/teacher/list/{tid}")
    @ApiOperation(value = "通过tid查询班级列表",notes = "通过tid查询班级列表")
    public Request queryClassesListByTid(@PathVariable("tid") Long tid){
        try {
            return new Request(true, StatusCode.OK,"通过tid查询班级列表成功", classesService.queryClassesListByTid(tid));
        }catch (ExamException e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"通过tid查询班级列表失败");
        }
    }

    @GetMapping("/teacher/stulist/{cid}")
    @ApiOperation(value = "通过cid查询学生列表",notes = "通过cid查询学生列表")
    public Request queryStuListByCid(@PathVariable("cid") Integer cid){
        try {
            return new Request(true, StatusCode.OK,"通过cid查询学生列表成功", classesService.queryStuListByCid(cid));
        }catch (ExamException e){
            System.out.println(e);
            return new Request(false, StatusCode.ERROR,"通过cid查询学生列表失败");
        }
    }

}
